<?php

try {
  $hostname = HOST_NAME;
  $username1 = USER_NAME2;
  $password1 = PASSWORD2;
  $dbname1 = DATABASE_NAME2;
  $myPdo1 = new PDO ('mysql:host=' . $hostname, $username1, $password1);
  $myPdo1->exec('USE ' . $dbname1);
  $myPdo1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  echo $e->getMessage ();
}

/*
 *get all data ,return array();
 * 
 * */
function getall($sql,$fetchmethod='')
{
    global $myPdo1;
    $data = array();
    
    $fetchmethod = empty($fetchmethod) ? PDO::FETCH_OBJ : PDO::FETCH_ASSOC ;
        
    $stmt2 = $myPdo1->prepare($sql);
    $stmt2->execute();

    while($tmp2 = $stmt2->fetch($fetchmethod)){
    	
        $data[] = $tmp2;
      }

      return $data;
}

/**
 *execute sql
 * */
function sql_execute($sql){
    global $myPdo1; 
    
    $stmt2 = $myPdo1->prepare($sql);
    $stmt2->execute();

    return;
}

/**
 *取得下一级的所有e_oid
 *$utable要查询的表,
 *$getfields 需要查询出来的字段,
 *$parentId 查询条件字段,
 *$e_oid 查询的值,
 *$condition=''其他条件,
 *$returnstr='' 返回类似于：1,2,3,4,5这种字符串
 */
 
 function getsonid($utable,$getfields,$parentId,$e_oid,$condition='',$returnstr='')
 {
    global $myPdo;  
    $data = array();
    $new_data = array();
    
    $sql = " select ".$getfields."  from  ".$utable." where ".$parentId." = '".$e_oid."'  ";
    if(!empty($condition)) $sql .= " and  ".$condition." ";
    
     $data = getall($sql,1);
     
        if(empty($returnstr))
        {
           if(!empty($data))
           {
                foreach($data as $value)
                {
                    $new_data[] = $value[$getfields];              
                }               
              
             
             
           }
           return $new_data;
        
        
        }else{
            $strid = '';
            
            if(!empty($data))
            {
               
                foreach($data as $value)
                {
                    $strid .= $value[$getfields].','; 
                }
                
                $strid = substr($strid,0,-1);
            }
            
            return $strid;          
        
        }
 }
 

    /*************************************produce operate**********************************************/
/**
 * pi_selects  operate
 * 
 */
           function pi_selects_operate($piselects,$debug='')
           {
                foreach ($piselects as $piselect)
                {
                    if(isset($piselect->id) || isset($piselect->e_oid))
                    {//update
                        $pisql ="update ns_produceselect set `trackId` = '".$piselect->trackId."',
                                                             `produce_trackId` = '".$piselect->produce_trackId."',
                                                             `title` = '".$piselect->title."',
                                                             `value` = '".$piselect->value."'
                                                        where `e_oid` = '".$piselect->e_oid."'";
                        if(!empty($debug))
                        {
                            echo $pisql."\n";
                        }else{
                            sql_execute($pisql);
                        } 
                        
                    }else{//add
                        $pisql = "insert into ns_produceselect(`e_oid`,`trackId`,`produce_trackId`,`title`,`value`) values(NULL,'".$piselect->trackId."','".$piselect->produce_trackId."',
                                                               '".$piselect->title."','".$piselect->value."')";
                        if(!empty($debug))
                        {
                            echo $pisql."\n";
                        }else{
                            sql_execute($pisql);
                        }                       
                        
                    }//end add
                    
                }   
          }
          
          
          /**
           * 
           * v2_purchaseItems operate
           * 
           */
          function v2_purchaseItems_operate($v2purchaseitem,$debug='')
          {
                foreach ($v2purchaseitem as $v2pitem)
                {
                    if(isset($v2pitem->id) || isset($v2pitem->e_oid))
                    {//update
                        $v2pitemsql ='update ns_producepurchaseitem set ';
                        foreach ($v2pitem as $key => $value)
                        {
                            if(!in_array($key,array('v2_selects','e_oid','id')))
                            {
                               $v2pitemsql .= '`'.$key."`='".addslashes($value)."',";
                            }
                        }
                        
                        $v2pitemsql =substr($v2pitemsql,0,-1);
                        
                        $v2pitemsql .= " where e_oid = '".$v2pitem->e_oid."'";
                        
                        if(!empty($debug))
                        {
                            echo $v2pitemsql."\n";
                        }else{
                            sql_execute($v2pitemsql);
                        }
                        unset($key);
                        unset($value);
                        
                            /**
                             * v2_selects
                             */
                              if(!empty($v2pitem->v2_selects))
                              {
                                  foreach ($v2pitem->v2_selects as $v2ptiemselect)
                                  {
                                     if(isset($v2ptiemselect->id) || isset($v2ptiemselect->e_oid))
                                     {//update
                                        $v2pitemselectsql = "update ns_producepurchaseitemselect set ";
                                        foreach ($v2ptiemselect as $key => $value)
                                        {
                                            if(!in_array($key,array('e_oid','id')))
                                            {
                                               $v2pitemselectsql .= '`'.$key."`='".addslashes($value)."',";
                                            }
                                        }
                                        $v2pitemselectsql =substr($v2pitemselectsql,0,-1);
                                        
                                        $v2pitemselectsql .= " where e_oid = '".$v2ptiemselect->id."'";  

                                        if(!empty($debug))
                                        {
                                            echo $v2pitemselectsql."\n";
                                        }else{
                                            sql_execute($v2pitemselectsql);
                                        }
                                        
                                     }else{//addd
                                        
                                        $v2pitemselectfield = '';
                                        $v2pitemselectvalue = '';
                                        
                                        foreach ($v2ptiemselect as $key => $value)
                                        {
                                            $v2pitemselectfield .='`'.$key.'`,';
                                            $v2pitemselectvalue .= " '".addslashes($value)."',"; 
                                            
                                        }
                                        
                                        $v2pitemselectfield = substr($v2pitemselectfield,0,-1);
                                        $v2pitemselectvalue = substr($v2pitemselectvalue,0,-1);  

                                        $v2pitemselectsql = "insert into ns_producepurchaseitemselect (`e_oid`,".$v2pitemselectfield.") values(NULL,".$v2pitemselectvalue.")";
                                        
                                        if(!empty($debug))
                                        {
                                            echo $v2pitemselectsql."\n";
                                        }else{
                                            sql_execute($v2pitemselectsql);
                                        }
                                     }//end add $v2ptiemselect
                                    
                                     unset($key);
                                     unset($value);
                                  }
                                
                                
                              }//not null
                              
                    }else{//purchaseitemadd
                        
                        $v2pitemfield = '';
                        $v2pitemvalue = '';
                        
                        foreach ($v2pitem as $key => $value)
                        {
                        	if(!in_array($key,array('v2_selects','selects','notes','_added','cancelled','isFree','urgent','order_id')))
                        	{
                        		$v2pitemfield .='`'.$key.'`,';
                                $v2pitemvalue .= " '".addslashes($value)."',"; 
                        	}
                        }
                        
                        $v2pitemfield = substr($v2pitemfield,0,-1);
                        $v2pitemvalue = substr($v2pitemvalue,0,-1);

                        $v2pitemsql = "insert into ns_producepurchaseitem(`e_oid`,".$v2pitemfield.") values(NULL,".$v2pitemvalue.")";  

                        if(!empty($debug))
                        {
                            echo $v2pitemsql."\n";
                        }else{
                            sql_execute($v2pitemsql);
                        }
                           unset($key);
                           unset($value);
                            /**
                             * $v2ptiemselect add
                             */
                             if(!empty($v2pitem->v2_selects))
                             {
                             	foreach ($v2pitem->v2_selects as $v2pitemselect)
                             	{
	                                $v2pitemselectfield = '';
	                                $v2pitemselectvalue = '';
	                                
	                                foreach ($v2pitemselect as $key => $value)
	                                {
	                                	if(!in_array($key,array('purchaseitem_id')))
	                                	{
	                                		$v2pitemselectfield .='`'.$key.'`,';
	                                		$v2pitemselectvalue .= " '".addslashes($value)."',";
	                                	}
	                                    
	                                }
	                                
	                                $v2pitemselectfield = substr($v2pitemselectfield,0,-1);
	                                $v2pitemselectvalue = substr($v2pitemselectvalue,0,-1);
	
	                                $v2pitemselectsql = "insert into ns_producepurchaseitemselect(`e_oid`,".$v2pitemselectfield.") values(NULL,".$v2pitemselectvalue.")";    
	
	                                if(!empty($debug))
	                                {
	                                    echo $v2pitemselectsql."\n";
	                                }else{
	                                    sql_execute($v2pitemselectsql);
	                                }
	                                unset($key);
	                                unset($value);
                                
                             	}//one v2purchaseitemselect
                             	
                             }//end v2purchaseitemselect add
                        
                    }//end purchaseitemadd
                    
                }           
            
          }
          
          /**
           * 
           * pUsers operate
           */
          function pusers_operate($pusers,$debug='')
          {
                foreach ($pusers as $puser)
                {
                    if(isset($puser->id) || isset($puser->e_oid))
                    {//update puser
                        $pusersql ='update ns_produceuser set ';
                        
                        foreach ($puser as $key => $value)
                        {
                            if( !in_array( $key,array('e_oid','id') ) )
                            {
                                $pusersql .= '`'.$key."`='".addslashes($value)."',";
                            }
                            
                        }
                        
                        $pusersql =substr($pusersql,0,-1);
                        
                        $pusersql .= " where e_oid = '".$puser->e_oid."'";
                        
                        if(!empty($debug))
                        {
                            echo $pusersql."\n";
                        }else{
                            sql_execute($pusersql);
                        }
                        unset($key);
                        unset($value);                            
                        
                    }else{//add puser
                        $puserfield = '';
                        $puservalue = '';
                        
                        foreach ($puser as $key => $value)
                        {
                            $puserfield .='`'.$key.'`,';
                            $puservalue .= " '".addslashes($value)."',";
                        }
                        
                        $puserfield = substr($puserfield,0,-1);
                        $puservalue = substr($puservalue,0,-1);

                        $pusersql = "insert into ns_produceuser(`e_oid`,".$puserfield.") values(NULL,".$puservalue.")";

                        if(!empty($debug))
                        {
                            echo $pusersql."\n";
                            
                        }else{
                            sql_execute($pusersql);
                            
                        }
                        unset($key);
                        unset($value);                      
                        
                    }//end puser
                    
                }           
            
            
          }
          
          /**
           * v2_events operate 
           * 
           */
          function v2_events_operate($v2event,$debug='')
          {
                foreach ($v2event as $v2event)
                {
                    if(isset($v2event->id) || isset($v2event->e_oid))
                    {//v2_events update
                        $v2eventsql = 'update ns_produceevent set '; 
                        foreach ($v2event as $key => $value)
                        {
                            if(!in_array($key,array('e_oid','id')))
                            {
                                if(in_array($key,array('date','isExpired')))
                                {
                                	if(is_object($v2event->$key))
                                	{
                                		$value = SimpleDate::toStamp($value);
                                		if(empty($value)) $value = 0;
                                	}else{
                                		if(empty($v2event->$key)) $value = 0;
                                	}
                                	
                                }
                                
                                $v2eventsql .= '`'.$key."`='".addslashes($value)."',";
                            }
                        }
                        
                        $v2eventsql =substr($v2eventsql,0,-1);
                        
                        $v2eventsql .= " where e_oid = '".$v2event->e_oid."'";
                        
                        if(!empty($debug))
                        {
                            echo $v2eventsql."\n";
                        }else{
                            sql_execute($v2eventsql);
                        }
                        unset($key);
                        unset($value);                          
                        
                    }else{//v2_events add
                        
                        $v2eventfield = '';
                        $v2eventvalue = '';
                        
                        foreach ($v2event as $key => $value)
                        {
                            $v2eventfield .='`'.$key.'`,';
                            if(in_array($key,array('date','isExpired')))
                            {
                            	if(is_object($v2event->$key))
                            	{
                            		$value = SimpleDate::toStamp($value);
                            		if(empty($value)) $value = 0;
                            	}else{
                            		if(empty($v2event->$key)) $value = 0;
                            	}
                            	
                            }
                            
                            $v2eventvalue .= " '".addslashes($value)."',"; 
                        }
                        
                        $v2eventfield = substr($v2eventfield,0,-1);
                        $v2eventvalue = substr($v2eventvalue,0,-1);

                        $v2eventsql = "insert into ns_produceevent(`e_oid`,".$v2eventfield.") values(NULL,".$v2eventvalue.")";  

                        if(!empty($debug))
                        {
                            echo $v2eventsql."\n";
                            
                        }else{
                            sql_execute($v2eventsql);
                            
                        }
                        unset($key);
                        unset($value);
                        
                    }//end events add
                }           
            
          }
          
          /**
           * v2_tracks operate
           */
          function v2_tracks_operate($v2tracks,$debug='')
          {
                foreach ($v2tracks as $v2tracks)
                {
                    if(isset($v2tracks->id) || isset($v2tracks->e_oid))
                    {//$v2tracks update
                        $v2tracksql = " update ns_producetrack set ";
                        foreach ($v2tracks as $key => $value)
                        {
                            if(!in_array($key,array('e_oid','pUsers','id','when')))
                            {
                                if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date')))
                                {
                                	if(is_object($v2tracks->$key))
                                	{
                                		$value = SimpleDate::toStamp($value);
                                	}else{
                                		if(empty($v2tracks->$key) ) $value = 0;
                                	}
                                }
                                $v2tracksql .= '`'.$key."`='".addslashes($value)."',";
                            }
                            
                           if(in_array($key,array('when')))
                           {
                               if(empty($v2tracks->when)) 
                               {
                                  $v2tracks->when = 0;
                                  $v2tracksql .= " `when` = '".$v2tracks->when."',";
                               }else{
                                  $v2tracksql .= " `when` = '".SimpleDate::toStamp($v2tracks->when)."',";                               
                               }
                               
                           }                                  
                            
                        }
                        
                        $v2tracksql =substr($v2tracksql,0,-1);
                        
                        $v2tracksql .= " where e_oid = '".$v2tracks->e_oid."'";
                        
                        if(!empty($debug))
                        {
                            echo $v2tracksql."\n";
                        }else{
                            sql_execute($v2tracksql);
                        }
                        unset($key);
                        unset($value);

                            /**
                             * pUsers operate
                             */
                             if(!empty($v2tracks->pUsers))
                             {
                                foreach ($v2tracks->pUsers as $v2tuser)
                                {
                                    if(isset($v2tuser->id) || isset($v2tuser->e_oid))
                                    {//update $v2tuser
                                        $v2tracksusersql ='update ns_producetrackuser set ';
                                        foreach ($v2tuser as $key => $value)
                                        {
                                            if(!in_array($key,array('e_oid','id','when')))
                                            {
                                                 $v2tracksusersql .= '`'.$key."`='".addslashes($value)."',";
                                                
                                            }
                                            
                                           if(in_array($key,array('when')))
                                           {
                                               if(empty($v2tuser->when))
                                               {
                                                  $v2tuser->when = 0;
                                                  $v2tracksusersql .= " `when` = '".$v2tuser->when."',";
                                               }else{
                                                  $v2tracksusersql .= " `when` = '".SimpleDate::toStamp($v2tuser->when)."',";                                               
                                               }
                                                    
                                           }                                               
                                            
                                        }
                                        
                                        $v2tracksusersql =substr($v2tracksusersql,0,-1);
                                        
                                        $v2tracksusersql .= " where e_oid = '".$v2tuser->e_oid."'";
                                        
                                        if(!empty($debug))
                                        {
                                            echo $v2tracksusersql."\n";
                                        }else{
                                            sql_execute($v2tracksusersql);
                                        }
                                        unset($key);
                                        unset($value);                                          
                                        
                                    }else{//add $v2tuser
                                        $v2tracksuserfield = '';
                                        $v2tracksuservalue = '';
                                        
                                        foreach ($v2tuser as $key => $value)
                                        {
                                            $v2tracksuserfield .='`'.$key.'`,';
                                            
                                            if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date')))
                                            {
                                            	if(is_object($v2tuser->$key))
                                            	{
                                            		$value = SimpleDate::toStamp($value);
                                            	}
                                            }
                                            
                                            $v2tracksuservalue .= " '".addslashes($value)."',"; 
                                        }
                                        
                                        $v2tracksuserfield = substr($v2tracksuserfield,0,-1);
                                        $v2tracksuservalue = substr($v2tracksuservalue,0,-1);

                                        $v2trackusersql = "insert into ns_producetrackuser(`e_oid`,".$v2tracksuserfield.") values(NULL,".$v2tracksuservalue.")";                                    
                                        
                                        if(!empty($debug))
                                        {
                                            echo $v2trackusersql."\n";
                                            
                                        }else{
                                            sql_execute($v2trackusersql);
                                            
                                        }
                                        unset($key);
                                        unset($value);
                                        
                                    }//add $v2tuser
                                    
                                }
                                
                             }//pusers not null
                        
                    }else{//$v2tracks add
                        $v2trackfield = '';
                        $v2trackvalue = '';
                        
                        foreach ($v2tracks as $key => $value)
                        {
                            
                            $v2trackfield .='`'.$key.'`,';

                            if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date','when')))
                            {
                            	if(is_object($v2tracks->$key))
                            	{
                            		$value = SimpleDate::toStamp($value);
                            	}
                            }
                        
                            $v2trackvalue .= " '".addslashes($value)."',";

                            if(in_array($key,array('when')))
                            {
                                $v2producefield .='`when`,'; 
                                
                                if(empty($v2tracks->when))
                                {
                                    $v2tracks->when = 0;
                                    $v2producevalue .= " '".$v2tracks->when."',";
                                }else{
                                    $v2producevalue .= " '".SimpleDate::toStamp($v2tracks->when)."',";                                  
                                }
                                
                            }                                   
                        }
                        
                        $v2trackfield = substr($v2trackfield,0,-1);
                        $v2trackvalue = substr($v2trackvalue,0,-1);

                        $v2tracksql = "insert into ns_producetrack(`e_oid`,".$v2trackfield.") values(NULL,".$v2trackvalue.")";  

                        if(!empty($debug))
                        {
                            echo $v2tracksql."\n";
                        }else{
                            sql_execute($v2tracksql);
                        }
                        unset($key);
                        unset($value);
                        
                          /**
                           * puser
                           */
                            if(!empty($v2tracks->pUsers) )
                            {
                                foreach ($v2tracks->pUsers as $v2tuser){
                                        
                                        $v2tracksuserfield = '';
                                        $v2tracksuservalue = '';
                                        
                                        foreach ($v2tuser as $key => $value)
                                        {
                                            $v2tracksuserfield .='`'.$key.'`,';
                                            
                                            if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date')))
                                            {
                                            	if(is_object($v2tuser->$key))
                                            	{
                                            		$value = SimpleDate::toStamp($value);
                                            	}
                                            }
                                            
                                            $v2tracksuservalue .= " '".addslashes($value)."',"; 
                                        }
                                        
                                        $v2tracksuserfield = substr($v2tracksuserfield,0,-1);
                                        $v2tracksuservalue = substr($v2tracksuservalue,0,-1);

                                        $v2trackusersql = "insert into ns_producetrackuser(`e_oid`,".$v2tracksuserfield.") values(NULL,".$v2tracksuservalue.")";                                    
                                        
                                        if(!empty($debug))
                                        {
                                            echo $v2trackusersql."\n";
                                            
                                        }else{
                                            sql_execute($v2trackusersql);
                                            
                                        }
                                        unset($key);
                                        unset($value);
                                }
                                
                            }//puser not null
                        
                        
                    }//end $v2tracks add
                }           
            
          }
          
          /**
           * v2_notes
           * 
           */
          function v2_notes_operate($v2note,$debug)
          {
                foreach ($v2note as $v2note)
                {
                    if(isset($v2note->id) || isset($v2note->e_oid))
                    {//note update
                        $notesql ='update ns_producenote set ';
                        foreach ($v2note as $key => $value)
                        {
                            if(!in_array($key,array('e_oid','id','_deleted')))
                            {
                                if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date')))
                                {
                                	if(is_object($v2note->$key))
                                	{
                                		$value = SimpleDate::toStamp($value);
                                	}
                                }
                                $notesql .= '`'.$key."`='".addslashes($value)."',";
                            }
                            
                        }
                        
                        $notesql =substr($notesql,0,-1);
                        
                        $notesql .= " where e_oid = '".$v2note->e_oid."'";
                        
                        if(!empty($debug))
                        {
                            echo $notesql."\n";
                        }else{
                            sql_execute($notesql);
                        }
                        unset($key);
                        unset($value);                          

                    }else{//note add
                        $notesfield = '';
                        $notevalue = '';
                        
                        foreach ($v2note as $key => $value)
                        {
                            $notesfield .='`'.$key.'`,'; 
                            
                            if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date')))
                            {
                            	if(is_object($v2note->$key))
                            	{
                            		$value = SimpleDate::toStamp($value);
                            	}
                            }
                            
                            $notevalue .= " '".addslashes($value)."',";
                        }

                        $notesfield = substr($notesfield,0,-1);
                        $notevalue = substr($notevalue,0,-1);

                        $notesql = "insert into ns_producenote(`e_oid`,".$notesfield.") values(NULL,".$notevalue.")";

                        if(!empty($debug))
                        {
                            echo $notesql."\n";
                        }else{
                            sql_execute($notesql);
                        }
                        unset($key);
                        unset($value);
                                    
                    }//note end add
                }           
            
          }

  















?>